package com.sdhs.projectstart.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.projectstart.domain.ProjectOther;
import com.sdhs.projectstart.mapper.ProjectOtherMapper;
import com.sdhs.projectstart.service.IProjectOtherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目其他信息 服务层
 *
 * @author ww
 */
@Service
public class ProjectOtherServiceImpl implements IProjectOtherService {
    @Autowired
    private ProjectOtherMapper projectDesignMapper;


    @Autowired
    private IBusinessFileService businessFileService;
    /**
     * 查询list
     *
     * @param projectOther 查询条件
     * @return 数组
     */
    @Override
    public List<ProjectOther> selectProjectOtherList(ProjectOther projectOther) {
        return projectDesignMapper.selectProjectOtherList(projectOther);
    }

    /**
     * 查询对象
     *
     * @param id 参数ID
     * @return 对象信息
     */
    @Override
    public ProjectOther selectProjectOtherById(Long id) {
        ProjectOther projectDesign = projectDesignMapper.selectProjectOtherById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_PROJECT_JOINT_DESIGN.getCode(), String.valueOf(id));
        projectDesign.setFileList(businessFiles);
        return projectDesign;
    }


    /**
     * 新增保存项目其他信息
     *
     * @param projectOther 对象信息
     * @return 结果
     */
    @Override
    public ProjectOther insertProjectOther(ProjectOther projectOther) {
        projectOther.setId(IdWorker.getId());
        projectDesignMapper.insertProjectOther(projectOther);
        handler(projectOther.getId(), projectOther);
        return projectOther;
    }

    /**
     * 新增保存项目其他信息
     *
     * @param projectOther 对象信息
     * @return 结果
     */
    @Override
    public int updateProjectOther(ProjectOther projectOther) {
        int i = projectDesignMapper.updateProjectOther(projectOther);
        handler(projectOther.getId(), projectOther);
        return i;
    }

    /**
     * 批量删除项目其他信息
     *
     * @param ids ids
     * @return 结果
     */
    @Override
    public int deleteProjectOtherByIds(Long[] ids) {
        return projectDesignMapper.deleteProjectOtherByIds(ids);
    }

    /**
     * 保存联合数据
     * 保存合同附件
     */
    public void handler(Long id, ProjectOther projectOther) {
        // 保存合同附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_PROJECT_JOINT_DESIGN.getCode(), String.valueOf(id), projectOther.getFileList());
    }
}
